package com.sj.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sj.entity.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * 评论表 Mapper 接口
 * </p>
 *
 * @author 孙峻
 * @since 2022-03-09
 */
@Mapper
@Repository
public interface CommentMapper extends BaseMapper<Comment> {

    /**
     * 获取文章的评论或是回复数
     *
     * @param id   文章id
     * @param type 评论或回复
     * @return
     */
    @Select("select count(1) from comment where article_id = #{id} and `type` = #{type} and del = 0 and parent_id = 0")
    Long getSumCommentNum(@Param("id") Integer id, @Param("type") String type);

    /**
     * 获取用户回答问题列表
     *
     * @param answerPage
     * @param id
     * @return
     */
    @Select("select t1.* from comment t1 left join article t2 on t1.article_id = t2.id where t1.del = 0 and t2.del = 0 and t2.user_id = #{id}")
    IPage<Comment> getUserAnswer(IPage<Comment> answerPage, @Param("id") Integer id);

    /**
     * 获取用户回答问题数量
     *
     * @param id
     * @return
     */
    @Select("select COUNT(*) AS total from comment t1 left join article t2 on t1.article_id = t2.id where t1.del = 0 and t2.del = 0 and t2.user_id = #{id}")
    Long getUserAnswerCount(Integer id);

    /**
     * 管理端获取评论列表
     *
     * @param type
     * @param page
     * @return
     */
    IPage<Comment> getAdminCommentList(IPage<Comment> page, @Param("type") String type);
}
